package cn.yunyichina.reservation.controller;

import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.constant.Redis;
import cn.yunyichina.reservation.vo.ConsoleUserVo;
import cn.yunyichina.reservation.vo.EnumVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Lullaby on 2016/8/24.
 */
public class BaseController {

    private static final String RESERVATION_ENUM_GET = "yyt.reservation.enum.get";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public ConsoleUserVo getConsoleUser(String username) {
        if (StringUtils.isNotBlank(username)) {
            String cacheKey = Redis.CONSOLE_SESSION_PREFIX.concat(username);
            String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
            if (StringUtils.isNotBlank(cacheValue)) {
                ConsoleUserVo user = Jackson.getBeanJsonData(cacheValue, ConsoleUserVo.class);
                return user;
            }
        }
        return null;
    }

    public List<EnumVo> getEnums(String enumCodes) {
        Map<String, String> params = new HashMap<>();
        params.put("enum_codes", enumCodes);
        return getEnums(params);
    }

    private List<EnumVo> getEnums(Map<String, String> params) {
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_ENUM_GET, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse != null) {
            return Jackson.getBeanJsonData(serviceResponse.getResult(), List.class);
        }
        return null;
    }

}
